package com.wm.exam.service.impl;

import com.wm.exam.dao.CategoryMapper;
import com.wm.exam.dao.PaperMapper;
import com.wm.exam.pojo.Category;
import com.wm.exam.pojo.Paper;
import com.wm.exam.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private PaperMapper paperMapper;

    /**
     * 查询分类是否存在
     *
     * @param clazz
     * @return
     */
    @Override
    public Category getByClazz(String clazz) {

        return categoryMapper.getByClazz(clazz);
    }

    /**
     * 添加分类到数据库
     * @param category
     * @return
     */
    @Override
    public int insertCategory(Category category, String uuid) {
        Category newCategory = new Category();
        newCategory.setTime(category.getTime());
        newCategory.setStatus(1);
        newCategory.setClazz(category.getClazz());
        newCategory.setAuthor(category.getAuthor());
        newCategory.setScope(category.getScope());
        newCategory.setUuid(uuid);
        return  categoryMapper.insertSelective(newCategory);
    }

    /**
     * 根据uuid查询
     * @param uuid
     * @return
     */
    @Override
    public Category getByUUID(String uuid) {
        return categoryMapper.getByUUID(uuid);
    }

    /**
     * 更新分类信息
     * @param category
     * @return
     */
    @Override
    public int updateById(Category category) {
        Category newCategory = new Category();
        newCategory.setAuthor(category.getAuthor());
        newCategory.setClazz(category.getClazz());
        newCategory.setScope(category.getScope());
        newCategory.setId(category.getId());
        newCategory.setTime(category.getTime());
        return categoryMapper.updateByPrimaryKeySelective(newCategory);
    }

    /**
     * 根据UUID删除
     * @param uuid
     * @return
     */
    @Override
    public int deleteyUUID(String uuid) {
        // 查询试题
        List<Paper> paperList = paperMapper.getByPaperUUID(uuid);
        if (null != paperList) {
            paperMapper.deleteByUUID(uuid);
        }
        // 删除分类

        return categoryMapper.deleteByUUID(uuid);
    }

}
